<?php

/**
 * @brief 优惠券模块
 * @class Coupon
 * @note  前台
 */
class Coupons extends IController
{
    public $layout = 'ucenter';

    public function init()
    {
    }

    function isSpecialPrivileges()
    {
        $tenantId    = strtolower(trim($this->user['tenant_id']));
        $privilieges = array('htsec');
        if (in_array($tenantId, $privilieges)) {
            return true;
        }

        return false;
    }

    /**
     * 个人中心优惠券
     */
    public function user_coupon_list()
    {
        $getToken  = IReq::get('token');
        $userToken = ISafe::get("user_zxl_token");
        $token     = $getToken ? urldecode(trim($getToken)) : $userToken;
        if (empty($token)) {
            IError::show(403, "用户信息不存在:参数异常");
            exit;
        }
        $zxlApi      = new ZxlApi($token);
        $userApiInfo = $zxlApi->getUserInfo(null);
        if (empty($userApiInfo)) {
            IError::show(403, "用户信息不存在：token异常");
            exit;
        }
        $userDb   = new IModel('user');
        $username = $userApiInfo['id'] . "@" . $userApiInfo['tenant_id'];
        $userInfo = $userDb->getObj("username='{$username}'");
        if ($userInfo['id']) {
            $couponObj = new coupon();
            //未使用
            $unUseCoupon = $couponObj->getUserCoupon($userInfo['id'], 0);
            //已使用
            $hasUseCoupon = $couponObj->getUserCoupon($userInfo['id'], 1);
            //已经过期 退款中 已退款
            $outDateCoupon = $couponObj->getUserCoupon($userInfo['id'],'', array(4,5,6));
            $data['un_use_coupon_count']   = $unUseCoupon ? count($unUseCoupon) : 0;
            $data['un_use_coupon_list']    = $unUseCoupon;
            $data['has_use_coupon_count']  = $hasUseCoupon ? count($hasUseCoupon) : 0;
            $data['has_use_coupon_list']   = $hasUseCoupon;
            $data['out_date_coupon_count'] = $outDateCoupon ? count($outDateCoupon) : 0;
            $data['out_date_coupon_list']  = $outDateCoupon;
            $this->setRenderData($data);
            $this->redirect('user_coupon_list');
        } else {
            IError::show(403, "用户信息不存在");
            exit;
        }
    }

    /**
     * 使用优惠券校验已经 使用优惠券后金额计算
     *
     */
    public function use_coupon_check()
    {
        $user_id      = ($this->user['user_id'] == null) ? 0 : $this->user['user_id'];
        $cartObj      = new Cart();
        $buyInfo      = $cartObj->getMyCart();
        $countSumObj  = new CountSum($user_id);
        $buyGoodsList = $countSumObj->goodsCount($buyInfo, '', '');
        $couponCode   = IFilter::act(IReq::get('coupon_code'));
        $couponObj    = new coupon();
        $result       = $couponObj->useCouponCheckAndBalance($buyGoodsList, $couponCode);
        $data         = array();
        if ($result !== false) {
            $data = array(
                'pay_point_sum'      => $result['pay_point_sum'],
                'pay_price_sum'      => $result['pay_price_sum'],
                'discount_point_sum' => $result['discount_point_sum'],
                'discount_price_sum' => $result['discount_price_sum'],
                'use_coupon_count'   => $result['use_coupon_count'],
                'use_coupon_code'    => trim($result['use_coupon_code'], ','),
            );
        }
        if ($couponObj->error) {
            die(JSON::encode(array(
                'code' => $couponObj->error['code'],
                'msg'  => $couponObj->error['msg'],
                'data' => $data
            )));
        } else {
            if ($data['use_coupon_code']) {
                $really_pay_point      = $result['pay_point_sum'] - $result['discount_point_sum'];
                $really_pay_price      = $result['pay_price_sum'] - $result['discount_price_sum'];
                $data['pay_point_sum'] = $really_pay_point > 0 ? $really_pay_point : 0;
                $data['pay_price_sum'] = $really_pay_price > 0 ? $really_pay_price : 0;
            }
            die(JSON::encode(array(
                'code' => 'success',
                'data' => $data
            )));
        }

    }

}